//
//  FDPersonController.m
//  FamousDoctorOnline
//
//  Created by 应谨鸿 on 16/7/8.
//  Copyright © 2016年 wszyxc. All rights reserved.
//

#import "FDPersonController.h"
//#import "FDPersonModel.h"
#import "FDUserInfoCell.h"

#import "FDSetUserInfoController.h"

@interface FDPersonController ()<UITableViewDataSource,UITableViewDelegate>

//固定数据
@property (strong,nonatomic) NSArray * dataArray;

@property (strong,nonatomic) NSMutableArray * descArray;

@property (nonatomic,strong) UITableView *listView;

@property (weak,nonatomic) UIImageView * headerView;

@property (strong,nonatomic) NSMutableDictionary * descDict;

@end


#define kHeaderHeight 150



@implementation FDPersonController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"个人信息";
    
    [self setupUI];
    
    [self setupLayout];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"JianTou"] style:UIBarButtonItemStylePlain target:self action:@selector(goBack)];
    
}

- (void)goBack{
    
    //保存数据
    
    [WZHUserDefaults WZHSaveObject:self.descArray forKey:@"userInfo"];
    
    //注册后的默认密码为FD
    [WZHUserDefaults WZHSaveObject:@"FD" forKey:@"password"];
   
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (UITableView *)listView{
    
    if (!_listView) {
        
        UITableView* listView = [[UITableView alloc]initWithFrame:CGRectMake(0, kHeaderHeight, SCREEN_WIDTH, SCREEN_HEIGHT-kHeaderHeight) style:UITableViewStylePlain];
        
        _listView = listView;
        
        _listView.dataSource = self;
        _listView.delegate = self;
        
        [_listView registerClass:[FDUserInfoCell class] forCellReuseIdentifier:@"info"];
        
    }
    return _listView;
}


- (void)setupUI{
    
    //添加headerView
    UIImageView *headerView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kHeaderHeight)];
    
    self.headerView = headerView;
    
    headerView.image = [UIImage imageNamed:@"h1"];
    
    [self.view addSubview:headerView];
    
    //添加tableview
    [self.view addSubview:self.listView];
    
}


- (void)setupLayout{
    
    
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.leading.trailing.offset(0);
        make.top.offset(64);
        make.height.offset(kHeaderHeight);
        
        
    }];
    
//    [self.listView mas_makeConstraints:^(MASConstraintMaker *make) {
//       
//        make.top.equalTo(self.headerView.mas_bottom).offset(0);
//        make.leading.trailing.bottom.offset(0);
//        make.bottom.offset(0);
//        make.width.offset(SCREEN_WIDTH);
//        make.height.offset(SCREEN_HEIGHT-self.headerView.frame.size.height);
//        
//    }];
    
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //重用cell
    FDUserInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"info"];
    
    if (indexPath.row == 0) {
        cell.iconImageView.hidden = NO;
        cell.descLabel.hidden = YES;
    }else{
        
        cell.iconImageView.hidden = YES;
        cell.descLabel.hidden = NO;
    }
    
    cell.infoLabel.text = self.dataArray[indexPath
                                         .row];
    cell.descLabel.text = self.descArray[indexPath.row];
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 80;
    }
    
    return 60;
    
}


#pragma mark - 代理

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.dataArray[indexPath.row] isEqualToString:@"用户ID"] || [self.dataArray[indexPath.row] isEqualToString:@"头像"]) {
        return;
    }
    
    
    FDSetUserInfoController *setUserInfoVC = [[FDSetUserInfoController alloc]init];
    
    setUserInfoVC.title = self.dataArray[indexPath.row];
    
//    setUserInfoVC.listMDict = self.descDict;
//    setUserInfoVC.listKey = self.dataArray[indexPath.row];
    
    setUserInfoVC.listArray = self.descArray;
    setUserInfoVC.index = indexPath.row;
    
    [self.navigationController pushViewController:setUserInfoVC animated:YES];
    
    
}



/**
 *  懒加载
 */
- (NSArray *)dataArray{
    
    if (!_dataArray) {
        _dataArray = @[@"头像",@"姓名",@"年龄",@"性别",@"城市",@"电话号码",@"用户名",@"用户ID"];
    }
    
    return _dataArray;
}

- (NSMutableArray *)descArray{
    
    if (!_descArray) {
        
        NSArray *userInfo = [WZHUserDefaults WZHObjectForKey:@"userInfo"];
        
        if (userInfo.count == self.dataArray.count) {
            self.descArray = [NSMutableArray arrayWithArray:userInfo];
        }else{
            
            _descArray = [NSMutableArray arrayWithArray:self.dataArray];
        }
        
    }
    
    return _descArray;
    
}

- (NSMutableDictionary *)descDict{
    
    if (!_descDict) {
        _descDict = [NSMutableDictionary dictionary];
        
        [self.dataArray enumerateObjectsUsingBlock:^(NSString *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            if (![obj isEqualToString:@"用户ID"]) {
                
                _descDict[obj] = obj;
            }
            
            
        }];
    }
    
    return _descDict;
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [self.listView reloadData];
    
}







@end
